package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.OperatorDao;
import com.qdlc.p2p.dal.dto.Operator;

@Repository
public class OperatorDaoImpl extends BaseDaoImpl<Operator> implements OperatorDao {

	public Operator userFind(long id) {
		return find(id);
	}

	@SuppressWarnings("rawtypes")
	public Operator getManagerByUserName(String userName) {
		String jpql = "from Operator where userName = ?1";
		Query query = em.createQuery(jpql);
		query.setParameter(1, userName);
		List list = query.getResultList();
		if (list != null && list.size() >= 1) {
			return (Operator) list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void updateStatus(long id, int status) throws Exception {
		String jpql = "Update Operator set status = :status where id = :id";
		Query query = em.createQuery(jpql);
		query.setParameter("id", id);
		query.setParameter("status", status);
		int result = query.executeUpdate();
		if(result != 1){
			throw new BusinessException("禁用/启用管理员失败！", 1);
		}
	}

	@Override
	public int countByName(String name) {
		return countByCriteria(QueryParam.getInstance().addParam("name", name));
	}
}
